import 'dart:convert';
import 'package:chathouse/constant/my_color.dart';
import 'package:chathouse/util/base_utils.dart';
import 'package:chathouse/widgets/form_widgets/widget_paras.dart';
import 'package:dropdown_button2/dropdown_button2.dart';
import 'package:flutter/material.dart';
import 'package:flutter_picker/picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../util/font_utils.dart';
import 'form_widgets.dart';

typedef LocationSelectWidget9ItemChange = Function(
    String val, List<int> selectedInt);

//問題類型9 地區選擇
class LocationSelectWidget9 extends StatefulWidget {
  String title;
  String? selectedName;
  List<int>? selectedInt;
  List<dynamic> dataSource = [];
  LocationSelectWidget9ItemChange itemChange;

  bool request;

  LocationSelectWidget9(
      this.title, this.selectedName, this.selectedInt, this.itemChange,
      {this.request = false, Key? key})
      : super(key: key) {
    dataSource = area_dist.map((e) {
      return e.toMap();
    }).toList();
  }

  @override
  State<LocationSelectWidget9> createState() => _LocationSelectWidget9State();
}

class _LocationSelectWidget9State extends State<LocationSelectWidget9> {
  @override
  Widget build(BuildContext context) {
    return Container(
        padding: EdgeInsets.only(left: 26.w, right: 26.w),
        child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
          Row(crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              widget.request
                  ? Text(
                      "*",
                      style: getTextStyle(18.sp, color: Colors.red),
                    )
                  : Container(),
              Expanded(
                  child: Text(
                widget.title ?? "",
                style: getTextStyle(18.sp, color: Colors.black),
              ))
            ],
          ),
          SizedBox(
            height: 12.w,
          ),
          Row(children: [
            Expanded(
              child: Container(
                height: INPUT_ITEM_HEIGHT.w,
                padding: EdgeInsets.symmetric(horizontal: 12.w),
                decoration: BoxDecoration(
                  border:
                      Border.all(color: MyColors.gary_158157157, width: 1.w),
                ),
                child: Row(
                  children: [
                    Expanded(
                      child: Text(
                        widget.selectedName ?? "請選擇",
                        style: getTextStyle(16.sp,
                            color: widget.selectedName == null
                                ? MyColors.gary_155155155
                                : Colors.black),
                      ),
                    ),
                    Icon(
                      Icons.keyboard_arrow_down_rounded,
                      size: 24.w,
                      color: MyColors.gary_112112112,
                    )
                  ],
                ),
              ).setOnClickListener(() {
                showPickerDialog(context);
              }),
            ),
          ])
        ]));
  }

  showPickerDialog(BuildContext context) {
    Picker(
        adapter: PickerDataAdapter<String>(pickerData: widget.dataSource),
        hideHeader: true,
        title: Text(
          "選擇地區",
          style:
              getTextStyle(18.sp, color: Colors.black, fontType: FontType.MID),
        ),
        cancelText: "取消",
        confirmText: "確認",
        cancelTextStyle:
            getTextStyle(16.sp, color: Colors.black, fontType: FontType.MID),
        confirmTextStyle:
            getTextStyle(16.sp, color: Colors.black, fontType: FontType.MID),
        height: 200,
        itemExtent: 32,
        selecteds: widget.selectedInt,
        textStyle: getTextStyle(15.sp,
            color: Colors.black, fontType: FontType.DEFAULT),
        selectedTextStyle: getTextStyle(15.sp,
            color: Colors.black, fontType: FontType.DEFAULT),
        columnFlex: [3, 3, 4],
        onConfirm: (Picker picker, List value) {
          debugPrint(value.toString());
          debugPrint(picker.getSelectedValues().toString());
          String selectedName = picker.getSelectedValues().toString();
          selectedName = selectedName.substring(1, selectedName.length - 1);
          var list = value.map((e) {
            return int.parse(e.toString());
          }).toList();
          setState(() {
            widget.selectedName = selectedName;
            widget.selectedInt = list;
          });
          widget.itemChange(widget.selectedName ?? "", list);
        }).showDialog(context);
  }
}
